<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件存储服务</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .content {
            padding: 30px;
        }

        .upload-section {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            text-align: center;
            border: 2px dashed #dee2e6;
            transition: all 0.3s ease;
        }

        .upload-section:hover {
            border-color: #667eea;
            background: #f0f2ff;
        }

        .upload-section.dragover {
            border-color: #667eea;
            background: #e8f0ff;
            transform: scale(1.02);
        }

        .upload-icon {
            font-size: 3em;
            color: #667eea;
            margin-bottom: 20px;
        }

        .file-input {
            display: none;
        }

        .upload-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 1.1em;
            cursor: pointer;
            transition: all 0.3s ease;
            margin: 10px;
        }

        .upload-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

        .upload-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .stats-section {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 15px;
            text-align: center;
        }

        .stat-card h3 {
            font-size: 2em;
            margin-bottom: 5px;
        }

        .stat-card p {
            opacity: 0.9;
        }

        .files-section h2 {
            margin-bottom: 20px;
            color: #333;
            font-size: 1.8em;
        }

        .file-list {
            display: grid;
            gap: 15px;
        }

        .file-item {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            transition: all 0.3s ease;
            border-left: 4px solid #667eea;
        }

        .file-item:hover {
            background: #e8f0ff;
            transform: translateX(5px);
        }

        .file-info {
            flex: 1;
        }

        .file-name {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
            word-break: break-all;
        }

        .file-meta {
            color: #666;
            font-size: 0.9em;
        }

        .file-actions {
            display: flex;
            gap: 10px;
        }

        .action-btn {
            padding: 8px 15px;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-size: 0.9em;
            transition: all 0.3s ease;
        }

        .download-btn {
            background: #28a745;
            color: white;
        }

        .download-btn:hover {
            background: #218838;
        }

        .delete-btn {
            background: #dc3545;
            color: white;
        }

        .delete-btn:hover {
            background: #c82333;
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            border: 1px solid #f5c6cb;
        }

        .success {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 10px;
            margin: 20px 0;
            border: 1px solid #c3e6cb;
        }

        .progress-bar {
            width: 100%;
            height: 6px;
            background: #e9ecef;
            border-radius: 3px;
            overflow: hidden;
            margin: 10px 0;
        }

        .progress-fill {
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            width: 0%;
            transition: width 0.3s ease;
        }

        @media (max-width: 768px) {
            .container {
                margin: 10px;
                border-radius: 15px;
            }

            .header h1 {
                font-size: 2em;
            }

            .content {
                padding: 20px;
            }

            .file-item {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }

            .file-actions {
                width: 100%;
                justify-content: space-between;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📁 文件存储服务</h1>
            <p>简单、安全、高效的文件存储解决方案</p>
        </div>

        <div class="content">
            <!-- 上传区域 -->
            <div class="upload-section" id="uploadSection">
                <div class="upload-icon">📤</div>
                <h3>拖拽文件到此处或点击选择文件</h3>
                <p>支持所有文件类型，最大50MB</p>
                <input type="file" id="fileInput" class="file-input" multiple>
                <button class="upload-btn" onclick="document.getElementById('fileInput').click()">
                    选择文件
                </button>
                <button class="upload-btn" id="uploadBtn" onclick="uploadFiles()" disabled>
                    上传文件
                </button>
                <div class="progress-bar" id="progressBar" style="display: none;">
                    <div class="progress-fill" id="progressFill"></div>
                </div>
            </div>

            <!-- 统计信息 -->
            <div class="stats-section" id="statsSection">
                <div class="stat-card">
                    <h3 id="fileCount">0</h3>
                    <p>文件总数</p>
                </div>
                <div class="stat-card">
                    <h3 id="totalSize">0 MB</h3>
                    <p>总存储大小</p>
                </div>
            </div>

            <!-- 文件列表 -->
            <div class="files-section">
                <h2>📋 文件列表</h2>
                <div id="fileList" class="file-list">
                    <div class="loading">加载中...</div>
                </div>
            </div>
        </div>
    </div>

    <script>
        let selectedFiles = [];

        // 页面加载时获取文件列表和统计信息
        document.addEventListener('DOMContentLoaded', function() {
            loadFiles();
            loadStats();
        });

        // 文件选择处理
        document.getElementById('fileInput').addEventListener('change', function(e) {
            selectedFiles = Array.from(e.target.files);
            updateUploadButton();
        });

        // 拖拽上传
        const uploadSection = document.getElementById('uploadSection');
        
        uploadSection.addEventListener('dragover', function(e) {
            e.preventDefault();
            uploadSection.classList.add('dragover');
        });

        uploadSection.addEventListener('dragleave', function(e) {
            e.preventDefault();
            uploadSection.classList.remove('dragover');
        });

        uploadSection.addEventListener('drop', function(e) {
            e.preventDefault();
            uploadSection.classList.remove('dragover');
            selectedFiles = Array.from(e.dataTransfer.files);
            updateUploadButton();
        });

        function updateUploadButton() {
            const uploadBtn = document.getElementById('uploadBtn');
            uploadBtn.disabled = selectedFiles.length === 0;
            uploadBtn.textContent = selectedFiles.length > 0 ? 
                `上传 ${selectedFiles.length} 个文件` : '上传文件';
        }

        async function uploadFiles() {
            if (selectedFiles.length === 0) return;

            const uploadBtn = document.getElementById('uploadBtn');
            const progressBar = document.getElementById('progressBar');
            const progressFill = document.getElementById('progressFill');

            uploadBtn.disabled = true;
            progressBar.style.display = 'block';

            let uploadedCount = 0;

            for (const file of selectedFiles) {
                const formData = new FormData();
                formData.append('file', file);

                try {
                    const response = await fetch('/api/upload', {
                        method: 'POST',
                        body: formData
                    });

                    const result = await response.json();
                    
                    if (result.success) {
                        uploadedCount++;
                        showMessage(`文件 "${file.name}" 上传成功！`, 'success');
                    } else {
                        showMessage(`文件 "${file.name}" 上传失败: ${result.error}`, 'error');
                    }
                } catch (error) {
                    showMessage(`文件 "${file.name}" 上传失败: ${error.message}`, 'error');
                }

                // 更新进度条
                const progress = (uploadedCount / selectedFiles.length) * 100;
                progressFill.style.width = progress + '%';
            }

            // 重置状态
            selectedFiles = [];
            document.getElementById('fileInput').value = '';
            updateUploadButton();
            progressBar.style.display = 'none';
            progressFill.style.width = '0%';

            // 重新加载文件列表和统计信息
            loadFiles();
            loadStats();
        }

        async function loadFiles() {
            try {
                const response = await fetch('/api/files');
                const result = await response.json();

                if (result.success) {
                    displayFiles(result.files);
                } else {
                    showMessage('加载文件列表失败: ' + result.error, 'error');
                }
            } catch (error) {
                showMessage('加载文件列表失败: ' + error.message, 'error');
            }
        }

        function displayFiles(files) {
            const fileList = document.getElementById('fileList');
            
            if (files.length === 0) {
                fileList.innerHTML = '<div class="loading">暂无文件</div>';
                return;
            }

            fileList.innerHTML = files.map(file => `
                <div class="file-item">
                    <div class="file-info">
                        <div class="file-name">${file.name}</div>
                        <div class="file-meta">
                            ${formatFileSize(file.size)} • 
                            ${new Date(file.uploadTime).toLocaleString()}
                        </div>
                    </div>
                    <div class="file-actions">
                        <button class="action-btn download-btn" onclick="downloadFile('${file.name}')">
                            下载
                        </button>
                        <button class="action-btn delete-btn" onclick="deleteFile('${file.name}')">
                            删除
                        </button>
                    </div>
                </div>
            `).join('');
        }

        async function loadStats() {
            try {
                const response = await fetch('/api/stats');
                const result = await response.json();

                if (result.success) {
                    document.getElementById('fileCount').textContent = result.stats.fileCount;
                    document.getElementById('totalSize').textContent = result.stats.totalSizeMB + ' MB';
                }
            } catch (error) {
                console.error('Failed to load stats:', error);
            }
        }

        function downloadFile(filename) {
            window.open(`/api/download/${encodeURIComponent(filename)}`, '_blank');
        }

        async function deleteFile(filename) {
            if (!confirm(`确定要删除文件 "${filename}" 吗？`)) {
                return;
            }

            try {
                const response = await fetch(`/api/files/${encodeURIComponent(filename)}`, {
                    method: 'DELETE'
                });

                const result = await response.json();

                if (result.success) {
                    showMessage('文件删除成功！', 'success');
                    loadFiles();
                    loadStats();
                } else {
                    showMessage('删除失败: ' + result.error, 'error');
                }
            } catch (error) {
                showMessage('删除失败: ' + error.message, 'error');
            }
        }

        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        function showMessage(message, type) {
            const messageDiv = document.createElement('div');
            messageDiv.className = type;
            messageDiv.textContent = message;
            
            const content = document.querySelector('.content');
            content.insertBefore(messageDiv, content.firstChild);

            setTimeout(() => {
                messageDiv.remove();
            }, 5000);
        }
    </script>
</body>
</html>
